package com.scanner.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import com.scanner.util.LogUtil;

import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class BaseApplication extends Application {
    private static BaseApplication application;
    /**
     * 全局使用线程池
     * 网络请求、耗时等线程操作均放入线程测操作
     */
    public static ExecutorService actionThreadPool = Executors.newFixedThreadPool(1);

    /**
     * 暂时用作存放相关activity,方便finish()掉相关的页面，
     * 注意调用该方法Add Activity后 onDestroy（）中必须  调用MyApplication.finishActivityWithClassName(this.getLocalClassName());
     * 否则存在内存泄露问题
     */
    public static ArrayList<Activity> activityListManage = new ArrayList<Activity>();

    @Override
    public void onCreate() {
        super.onCreate();
        application=this;
        //Log开关
        LogUtil.init(LogUtil.VERBOSE,"jsb_scanner");

    }

    public static Context getEmApplicationContext() {
        return application;
    }

    public static BaseApplication getInstance() {
        return application;
    }

    /**
     * 根据类名finish其存放的activity
     *
     * @param className 类名
     */
    public static void finishActivityWithClassName(String className) {
        for (Activity activity : activityListManage) {
            if (activity != null && activity.getClass().getSimpleName().contains(className)) {
                activity.finish();
                activity = null;
            }
        }
    }

}
